﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abreu.Profile.Database;
using Abreu.Profile.ObjectClasses;
using System.Collections.ObjectModel;
using System.Data.Linq;

[assembly: CLSCompliant(true)]
namespace Abreu.Profile.Classes
{
    public class UserProfile : IDisposable 
    {
        #region Class Members
        CorpAbreuDataContext _corpAbreuDC = new CorpAbreuDataContext(); 
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the user profile isMaster info using the userId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns>ProfileInfo struct</returns>
        public ProfileInfo GetUserProfile(int? userId)
        {
            ProfileInfo profInfoList = null;
            Collection<PermissionProfile> profPermissionList = new Collection<PermissionProfile>();

            //Get the profile info with P_ProfileUser_Sel
            var profileInfo = _corpAbreuDC.P_GetProfileUserInfo(userId);

            foreach (P_GetProfileUserInfoResult resProfileInfo in profileInfo)
            {
                //Get PermissionInfo
                profPermissionList = GetPermissionList(resProfileInfo.UserId, resProfileInfo.SectionId, resProfileInfo.CategoryId);

                //Set ProfileInfo object with result
                profInfoList = new ProfileInfo(resProfileInfo.EmployeeId, resProfileInfo.SectionId, resProfileInfo.CategoryId, resProfileInfo.ReferenceSectionId,
                                    resProfileInfo.UserId, resProfileInfo.ReferenceCategoryId, resProfileInfo.BeginDateEmployeeUser, resProfileInfo.EndDateEmployeeUser,
                                    resProfileInfo.IsMaster, resProfileInfo.StatusEmployeeUser, resProfileInfo.DepartmentId, resProfileInfo.BusinessUnitId,
                                    resProfileInfo.NameSection, resProfileInfo.DocDefaultLanguageId, resProfileInfo.StatusSection, resProfileInfo.Code,
                                    resProfileInfo.NameBusinessUnit, resProfileInfo.BusinessUnitDescription, resProfileInfo.Level, resProfileInfo.StatusBusinessUnit,
                                    resProfileInfo.CategoryTypeId, resProfileInfo.NameCategory, resProfileInfo.CategoryDescription, resProfileInfo.Order,
                                    resProfileInfo.IsSectionDefined, resProfileInfo.BeginDateCategory, resProfileInfo.EndDateCategory, resProfileInfo.StatusCategory,
                                    resProfileInfo.LanguageId, resProfileInfo.UserTypeId, resProfileInfo.LogOn, resProfileInfo.AuthorizationCode, resProfileInfo.NameUser,
                                    resProfileInfo.Initials, resProfileInfo.Email, resProfileInfo.BeginDateUser, resProfileInfo.EndDateUser, resProfileInfo.StatusUser,
                                    profPermissionList);
            }
 
            //Return ProfileInfo
            return profInfoList;
        }

        /// <summary>
        /// Gets the user profile isMaster info using the userId and the sectionId
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="sectionId"></param>
        /// <returns>ProfileInfo struct</returns>
        public ProfileInfo GetUserProfileBySection(int? userId, int? sectionId)
        {
            ProfileInfo profInfoList = null;
            Collection<PermissionProfile> profPermissionList = new Collection<PermissionProfile>();

            //Get the profile info with P_ProfileUserSection_Sel
            var profileInfo = _corpAbreuDC.P_GetProfileUserSection(userId, sectionId);

            foreach (P_GetProfileUserSectionResult resProfileInfo in profileInfo)
            {
                //Get PermissionInfo
                profPermissionList = GetPermissionList(resProfileInfo.UserId, resProfileInfo.SectionId, resProfileInfo.CategoryId);

                //Set ProfileInfo object with result
                profInfoList = new ProfileInfo(resProfileInfo.EmployeeId, resProfileInfo.SectionId, resProfileInfo.CategoryId, resProfileInfo.ReferenceSectionId,
                                        resProfileInfo.UserId, resProfileInfo.ReferenceCategoryId, resProfileInfo.BeginDateEmployeeUser, resProfileInfo.EndDateEmployeeUser,
                                        resProfileInfo.IsMaster, resProfileInfo.StatusEmployeeUser, resProfileInfo.DepartmentId, resProfileInfo.BusinessUnitId,
                                        resProfileInfo.NameSection, resProfileInfo.DocDefaultLanguageId, resProfileInfo.StatusSection, resProfileInfo.Code,
                                        resProfileInfo.NameBusinessUnit, resProfileInfo.BusinessUnitDescription, resProfileInfo.Level, resProfileInfo.StatusBusinessUnit,
                                        resProfileInfo.CategoryTypeId, resProfileInfo.NameCategory, resProfileInfo.CategoryDescription, resProfileInfo.Order,
                                        resProfileInfo.IsSectionDefined, resProfileInfo.BeginDateCategory, resProfileInfo.EndDateCategory, resProfileInfo.StatusCategory,
                                        resProfileInfo.LanguageId, resProfileInfo.UserTypeId, resProfileInfo.LogOn, resProfileInfo.AuthorizationCode, resProfileInfo.NameUser,
                                        resProfileInfo.Initials, resProfileInfo.Email, resProfileInfo.BeginDateUser, resProfileInfo.EndDateUser, resProfileInfo.StatusUser,
                                        profPermissionList);
            }

            //Return ProfileInfo
            return profInfoList;
        }

        /// <summary>
        /// Gets all the information of user profile using userId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns>Collection of ProfileInfo</returns>
        public Collection<ProfileInfo> GetAllUserProfileInfo(int userId)
        {
            Collection<ProfileInfo> profInfoList = new Collection<ProfileInfo>();
            Collection<PermissionProfile> profPermissionList = new Collection<PermissionProfile>();

            //Get the profile info with P_ProfileUserAll_Sel
            var profileInfo = _corpAbreuDC.P_GetProfileUserAll(userId);

            foreach (P_GetProfileUserAllResult resProfileInfo in profileInfo)
            {
                //Get PermissionInfo
                profPermissionList = GetPermissionList(resProfileInfo.UserId, resProfileInfo.SectionId, resProfileInfo.CategoryId);

                //Set ProfileInfo object with result
                profInfoList.Add(new ProfileInfo(resProfileInfo.EmployeeId, resProfileInfo.SectionId, resProfileInfo.CategoryId, resProfileInfo.ReferenceSectionId,
                                    resProfileInfo.UserId, resProfileInfo.ReferenceCategoryId, resProfileInfo.BeginDateEmployeeUser, resProfileInfo.EndDateEmployeeUser,
                                    resProfileInfo.IsMaster, resProfileInfo.StatusEmployeeUser, resProfileInfo.DepartmentId, resProfileInfo.BusinessUnitId,
                                    resProfileInfo.NameSection, resProfileInfo.DocDefaultLanguageId, resProfileInfo.StatusSection, resProfileInfo.Code,
                                    resProfileInfo.NameBusinessUnit, resProfileInfo.BusinessUnitDescription, resProfileInfo.Level, resProfileInfo.StatusBusinessUnit,
                                    resProfileInfo.CategoryTypeId, resProfileInfo.NameCategory, resProfileInfo.CategoryDescription, resProfileInfo.Order,
                                    resProfileInfo.IsSectionDefined, resProfileInfo.BeginDateCategory, resProfileInfo.EndDateCategory, resProfileInfo.StatusCategory,
                                    resProfileInfo.LanguageId, resProfileInfo.UserTypeId, resProfileInfo.LogOn, resProfileInfo.AuthorizationCode, resProfileInfo.NameUser,
                                    resProfileInfo.Initials, resProfileInfo.Email, resProfileInfo.BeginDateUser, resProfileInfo.EndDateUser, resProfileInfo.StatusUser,
                                    profPermissionList));

            }

            //Return ProfileInfo
            return profInfoList;
        }

        /// <summary>
        /// Gets all the information of user profile using userId and sectionId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns>Collection of ProfileInfo</returns>
        public Collection<ProfileInfo> GetAllUserProfileInfoBySection(int userId, int sectionId)
        {
            Collection<ProfileInfo> profInfoList = new Collection<ProfileInfo>();
            Collection<PermissionProfile> profPermissionList = new Collection<PermissionProfile>();

            //Get the profile info with P_ProfileUserAll_Sel
            var profileInfo = _corpAbreuDC.P_GetProfileUserSectionAll(userId, sectionId);

            foreach (P_GetProfileUserSectionAllResult resProfileInfo in profileInfo)
            {
                //Get PermissionInfo
                profPermissionList = GetPermissionList(resProfileInfo.UserId, resProfileInfo.SectionId, resProfileInfo.CategoryId);

                //Set ProfileInfo object with result
                profInfoList.Add(new ProfileInfo(resProfileInfo.EmployeeId, resProfileInfo.SectionId, resProfileInfo.CategoryId, resProfileInfo.ReferenceSectionId,
                                    resProfileInfo.UserId, resProfileInfo.ReferenceCategoryId, resProfileInfo.BeginDateEmployeeUser, resProfileInfo.EndDateEmployeeUser,
                                    resProfileInfo.IsMaster, resProfileInfo.StatusEmployeeUser, resProfileInfo.DepartmentId, resProfileInfo.BusinessUnitId,
                                    resProfileInfo.NameSection, resProfileInfo.DocDefaultLanguageId, resProfileInfo.StatusSection, resProfileInfo.Code,
                                    resProfileInfo.NameBusinessUnit, resProfileInfo.BusinessUnitDescription, resProfileInfo.Level, resProfileInfo.StatusBusinessUnit,
                                    resProfileInfo.CategoryTypeId, resProfileInfo.NameCategory, resProfileInfo.CategoryDescription, resProfileInfo.Order,
                                    resProfileInfo.IsSectionDefined, resProfileInfo.BeginDateCategory, resProfileInfo.EndDateCategory, resProfileInfo.StatusCategory,
                                    resProfileInfo.LanguageId, resProfileInfo.UserTypeId, resProfileInfo.LogOn, resProfileInfo.AuthorizationCode, resProfileInfo.NameUser,
                                    resProfileInfo.Initials, resProfileInfo.Email, resProfileInfo.BeginDateUser, resProfileInfo.EndDateUser, resProfileInfo.StatusUser,
                                    profPermissionList));

            }

            //Return ProfileInfo
            return profInfoList;
        }

        /// <summary>
        /// Dispose members of class
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);

        }
        #endregion

        #region Private Methods

        private Collection<PermissionProfile> GetPermissionList(int? userId, int? sectionId, int? categoryId)
        {
            Collection<PermissionProfile> profPermissionList = new Collection<PermissionProfile>();

            //Get PermissionInfo with P_ProfilePermission_Sel
            var permissionInfo = _corpAbreuDC.P_GetProfilePermissionInfo(userId, sectionId, categoryId);

            foreach (P_GetProfilePermissionInfoResult resProfilePermission in permissionInfo)
            {
                profPermissionList.Add(new PermissionProfile(resProfilePermission.FunctionalityId, resProfilePermission.ApplicationId,
                                        resProfilePermission.GroupId, resProfilePermission.MenuId, resProfilePermission.Deny,
                                        resProfilePermission.StatusPermission));
            }

            //Returns Permission List 
            return profPermissionList;
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_corpAbreuDC != null)
                {
                    _corpAbreuDC.Dispose();
                    _corpAbreuDC = null;
                }
            }
        }
        #endregion
    }
}
